﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eloquera.Client;
using Model.Messaging;
using Model;


namespace _430GroupProject.Messaging
{
    

    public class MessageService
    {
        private static String dbErrorString = "Database Retrieval Error.";
        /// <summary>
        /// Sends the given message to the message.to users.
        /// </summary>
        /// <param name="message">The message to be sent.</param>
        public static ErrorMessage sendMessage(Message message){
            DB db = null;
            try
            {
                db = DataBase.getConnection();
                db.Store(message);
                db.Close();
            }
            catch (Exception) {
                checkDB(db);
                ErrorMessage em = new ErrorMessage();
                em.Header = dbErrorString;
                em.Message = "Unable to send message.";
            }
            return null;
        }

        /// <summary>
        /// Retrieves all message from the given user.
        /// </summary>
        /// <param name="userID">The user to sent the messages.</param>
        /// <returns>A list of messages that the given user has sent.</returns>
        public static Messages getMessagesFrom(String userID){
            DB db = null;
            Messages ret = new Messages();
            try
            {
                db = DataBase.getConnection();
                var messages = db.ExecuteQuery("SELECT Message WHERE From = '" + userID + "'");
                ret = new Messages();
                ret.AllMessages = new List<Message>();
                foreach (Message i in messages)
                {
                    ret.AllMessages.Add(i);
                }

                db.Close();
            }
            catch (Exception)
            {
                checkDB(db);

                ErrorMessage em = new ErrorMessage();
                em.Header = dbErrorString;
                em.Message = "Unable to retrieve message.";
                ret.Error = em;

            }
            return ret;
        }


        /// <summary>
        /// Retrieves all messages to the given user.
        /// </summary>
        /// <param name="userID">THe user to recived the messages.</param>
        /// <returns>A list of messages that the given user has recieved.</returns>
        public static Messages getMessageTo(String userID) {
            Messages ret = new Messages();
            DB db = null;
            try
            {
                db = DataBase.getConnection();

                var messages = db.ExecuteQuery("SELECT Message");

                ret.AllMessages = new List<Message>();
                foreach (Message i in messages)
                {
                    if (i.To.Contains(userID))
                    {
                        ret.AllMessages.Add(i);
                    }
                }
                db.Close();
            }catch(Exception){
                checkDB(db);
                ErrorMessage em = new ErrorMessage();
                em.Header = dbErrorString;
                em.Message = "Unable to retrieve messages from DB.";
                ret.Error = em;
            }

            return ret;
        }
           
        /// <summary>
        /// Updates a message in the database when its contents have been changed.
        /// For example if a user reads a message can marks it as read, the message will need to be updated in the DB.
        /// </summary>
        /// <param name="message">The message whos contents have changed.</param>
        public static ErrorMessage updateMessage(Message message) {
            DB db = null;
            try
            {
                db = DataBase.getConnection();
                db.Store(message);
                db.Close();
            }
            catch (Exception) {
                checkDB(db);
                ErrorMessage em = new ErrorMessage();
                em.Header = dbErrorString;
                em.Message = "Unable to update Message in database.";
            }
            return null;
        }

        private static void checkDB(DB db)
        {
            if (db != null && db.IsOpen)
                db.Close();
        }
    }
}
